package com.wgj.android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.Poi;
import com.baidu.mapapi.search.core.PoiInfo;
import com.wgj.android.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/2/6.
 */

public class MapAdapter extends RecyclerView.Adapter<MapAdapter.MyViewHolder> implements View.OnClickListener{
    private List<PoiInfo> pois;
    private Context context;
    private LayoutInflater inflater;
    public MapAdapter(Context context , List pois){
        this.context = context;

        this.inflater = LayoutInflater.from(context);
        if(pois==null){
            pois=new ArrayList<>();
        }
        this.pois=pois;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.map_item,null);
        view.setOnClickListener(this);
        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.location.setText(pois.get(position).name);
        holder.address.setText(pois.get(position).address);
        //holder.name.setText(pois.get(position).getId());
        holder.itemView.setTag(position);
        if(pois.get(position).hasCaterDetails){
            holder.image.setVisibility(View.VISIBLE);
        }else {
            holder.image.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return pois.size();
    }
    private OnItemClickListener mOnItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public class MyViewHolder extends  RecyclerView.ViewHolder{
        public TextView location;
        public ImageView image;
        public TextView address;

       // private TextView name;
        public MyViewHolder(View view) {
            super(view);
            location=view.findViewById(R.id.location_name);
            image=view.findViewById(R.id.image);
            address=view.findViewById(R.id.address);
            //name=view.findViewById(R.id.name);
        }

    }
}
